//
//  AnotherViewController.m
//  Shanghai Cuisine
//
//  Created by iOS Two on 14/12/18.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "AnotherViewController.h"
#import "myData.h"
#import "myObj.h"
#import "AppDelegate.h"

@interface AnotherViewController ()

@end

@implementation AnotherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getASIData];
    UILabel *titelLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, 44)];
    titelLabel.text = @"菜式详解";
    titelLabel.textAlignment = NSTextAlignmentCenter;
    titelLabel.font = [UIFont systemFontOfSize:19];
    titelLabel.textColor = [UIColor colorWithRed:0.97 green:0.69 blue:0.17 alpha:1];
    self.navigationItem.titleView = titelLabel;
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.9 green:0.64 blue:0.16 alpha:1];
    // Do any additional setup after loading the view.
}
-(void)buttonClick {
    [MobClick event:@"favorButton"];
    
    myData *mydata=[[myData alloc]init];
    [mydata initData];
    if (!_isExist) {
        [mydata insertData:_titleName title:_coverUrl info:_tips tableName:@"dishName" introduction:_intro smallImg:_coverUrl];
        for (int i = 0; i < _stepPhoto.count; i++) {
            [mydata insertData:_titleName title:[_stepPhoto objectAtIndex:i] info:[_stepIntro objectAtIndex:i] tableName:@"howToDo" introduction:nil smallImg:nil];
            UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"img_favor2"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick)];
            self.navigationItem.rightBarButtonItem = right;
        }
        for (int i = 0; i < _mainStuffWeight.count; i++) {
            [mydata insertData:_titleName title:[_mainStuffName objectAtIndex:i] info:[_mainStuffWeight objectAtIndex:i] tableName:@"mainMaterial" introduction:nil smallImg:nil];
        }
        for (int i = 0; i < _otherStuffName.count; i++) {
            [mydata insertData:_titleName title:[_otherStuffName objectAtIndex:i] info:[_otherStuffWeight objectAtIndex:i] tableName:@"auxiliaryMaterial" introduction:nil smallImg:nil];
        }
        [self alertViewShow:@"收藏成功"];
        _isExist = YES;
    } else {
        [mydata deleteTestList:_titleName tableName:@"dishName"];
        [mydata deleteTestList:_titleName tableName:@"howToDo"];
        [mydata deleteTestList:_titleName tableName:@"mainMaterial"];
        [mydata deleteTestList:_titleName tableName:@"auxiliaryMaterial"];
        UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"img_favor1"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick)];
        self.navigationItem.rightBarButtonItem = right;
        _isExist = NO;
        [self alertViewShow:@"取消收藏"];
    }
}

-(void)getASIData {
    [self showIndeterminateProgress];
    NSString *string = @"http://api.hoto.cn/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&deviceid=0F607264FC6318A92B9E13C65DB7CD3C%7C7DB64D6A-18DB-4843-BAD9-0D906C652209%7CC73A6469-0555-4A64-AC37-3A540302FBD0&format=json&loguid=&method=Info.getInfo&sessionid=1418805557&uuid=353ED12DEEE49FC5F5DF6F2CB3EC5665&vc=31&vn=v4.5.0&rid=";
    string = [string stringByAppendingString:[NSString stringWithFormat:@"%@",_recipeId]];
    
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:[NSURL URLWithString:string]];
    request.username = @"1";
    request.delegate = self;
    [request setShouldContinueWhenAppEntersBackground:YES];
    [request setTimeOutSeconds:60.0f];
    [request setDownloadCache:appDelegate.imageCache];
    [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
    [request startAsynchronous];
}
-(void)requestFinished:(ASIHTTPRequest *)request {
    _stepIntro = [[NSMutableArray alloc]init];
    _stepPhoto = [[NSMutableArray alloc]init];
    _mainStuffName = [[NSMutableArray alloc]init];
    _mainStuffWeight = [[NSMutableArray alloc]init];
    _otherStuffName = [[NSMutableArray alloc]init];
    _otherStuffWeight = [[NSMutableArray alloc]init];
    SBJsonParser *jsonP = [[SBJsonParser alloc]init];
    if ([request.username isEqualToString:@"1"]) {
        NSDictionary *dic = [[[jsonP objectWithData:[request responseData]] objectForKey:@"result"] objectForKey:@"info"];
        _coverUrl = [dic objectForKey:@"Cover"];
        _titleName = [dic objectForKey:@"Title"];
        _intro = [dic objectForKey:@"Intro"];
        _tips = [dic objectForKey:@"Tips"];
        for (int i = 0; i < [[dic objectForKey:@"Steps"] count]; i++) {
            [_stepIntro addObject:[[[dic objectForKey:@"Steps"] objectAtIndex:i]objectForKey:@"Intro"]];
            [_stepPhoto addObject:[[[dic objectForKey:@"Steps"] objectAtIndex:i]objectForKey:@"StepPhoto"]];
        }
        for (int i = 0; i < [[dic objectForKey:@"MainStuff"] count]; i++) {
            [_mainStuffName addObject:[[[dic objectForKey:@"MainStuff"] objectAtIndex:i] objectForKey:@"name"]];
            [_mainStuffWeight addObject:[[[dic objectForKey:@"MainStuff"] objectAtIndex:i] objectForKey:@"weight"]];
        }
        for (int i = 0; i < [[dic objectForKey:@"OtherStuff"] count]; i++) {
            [_otherStuffName addObject:[[[dic objectForKey:@"OtherStuff"] objectAtIndex:i] objectForKey:@"name"]];
            [_otherStuffWeight addObject:[[[dic objectForKey:@"OtherStuff"] objectAtIndex:i] objectForKey:@"weight"]];
        }
    }
    [HUD hide:YES];
    [self creatController];
    
    myData *mydata = [[myData alloc]init];
    [mydata initData];
    NSArray *array = [mydata selectData];
    if (array.count == 0) {
        UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"img_favor1"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick)];
        self.navigationItem.rightBarButtonItem = right;
        _isExist = NO;
    } else {
        for (int i = 0; i < array.count; i++) {
            myObj *obj = [array objectAtIndex:i];
            if ([obj.nameString isEqualToString:_titleName]) {
                UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"img_favor2"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick)];
                self.navigationItem.rightBarButtonItem = right;
                _isExist = YES;
                return;
            } else {
                UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"img_favor1"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick)];
                self.navigationItem.rightBarButtonItem = right;
                _isExist = NO;
            }
        }
    }
}
-(void)creatController {
    _scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kHeight)];
    _scroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_scroll];
    
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(159, 50, kwidth - 165, 50)];
    name.font = [UIFont systemFontOfSize:20];
    name.text = _titleName;
    name.numberOfLines = 0;
    [name sizeToFit];
    [_scroll addSubview:name];
    
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10, 150, 110)];
    [image setImageURLStr:_coverUrl placeholder:[UIImage imageNamed:@"img_delete_advertisement"]];
    [_scroll addSubview:image];
    
    UIImageView *image1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 130, kwidth, 32)];
    image1.image = [UIImage imageNamed:@"img_jianjie"];
    [_scroll addSubview:image1];
    
    UILabel *introduction = [[UILabel alloc]initWithFrame:CGRectMake(10, 172, 300, 20)];
    introduction.text = [[NSString stringWithFormat:@"%@", _intro] stringByReplacingOccurrencesOfString:@"\\n" withString:@" \r\n"];;
    introduction.font = [UIFont systemFontOfSize:13];
    introduction.numberOfLines = 0;
    [introduction sizeToFit];
    [_scroll addSubview:introduction];
    
    UIImageView *image2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, introduction.frame.size.height + introduction.frame.origin.y + 10, kwidth, 32)];
    image2.image = [UIImage imageNamed:@"img_zhuliao"];
    [_scroll addSubview:image2];
    
    _materialTable = [[UITableView alloc]initWithFrame:CGRectMake(0, image2.frame.size.height + image2.frame.origin.y, kwidth, 200)];
    if (_mainStuffName.count % 2 == 1) {
        _materialTable.frame = CGRectMake(0, image2.frame.size.height + image2.frame.origin.y, kwidth, (_mainStuffName.count / 2 + 1) * 50 );
    } else {
        _materialTable.frame = CGRectMake(0, image2.frame.size.height + image2.frame.origin.y, kwidth, (_mainStuffName.count / 2) * 50 );
    }
    _materialTable.scrollEnabled = NO;
    _materialTable.delegate = self;
    _materialTable.dataSource = self;
    _materialTable.tag = 1;
    _materialTable.rowHeight = 50;
    [_scroll addSubview:_materialTable];
    
    UIImageView *image3 = [[UIImageView alloc]initWithFrame:CGRectMake(0, _materialTable.frame.origin.y + _materialTable.frame.size.height - 1, kwidth, 32)];
    image3.image = [UIImage imageNamed:@"img_fuliao"];
    [_scroll addSubview:image3];
    
    _auxiliaryTable = [[UITableView alloc]initWithFrame:CGRectMake(0, image3.frame.origin.y + image3.frame.size.height, kwidth, 200)];
    if (_otherStuffName.count % 2 == 1) {
        _auxiliaryTable.frame = CGRectMake(0, image3.frame.origin.y + image3.frame.size.height, kwidth, (_otherStuffName.count / 2 + 1) * 50 );
    } else {
        _auxiliaryTable.frame = CGRectMake(0, image3.frame.origin.y + image3.frame.size.height, kwidth, (_otherStuffName.count / 2) * 50 );
    }
    _auxiliaryTable.scrollEnabled = NO;
    _auxiliaryTable.delegate = self;
    _auxiliaryTable.dataSource = self;
    _auxiliaryTable.tag = 3;
    _auxiliaryTable.rowHeight = 50;
    [_scroll addSubview:_auxiliaryTable];
    
    _image4 = [[UIImageView alloc]initWithFrame:CGRectMake(0, _auxiliaryTable.frame.size.height + _auxiliaryTable.frame.origin.y - 1, kwidth, 32)];
    _image4.image = [UIImage imageNamed:@"img_zhizuo"];
    [_scroll addSubview:_image4];
    
    _startTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 32 + _image4.frame.origin.y , kwidth, _stepPhoto.count * 70)];
    _startTable.scrollEnabled = NO;
    _startTable.delegate = self;
    _startTable.dataSource = self;
    _startTable.tag = 2;
    _startTable.rowHeight = 70;
    _startTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_scroll addSubview:_startTable];
    
    UIImageView *image4 = [[UIImageView alloc]initWithFrame:CGRectMake(0, _startTable.frame.origin.y + 16 + _startTable.frame.size.height - 1, kwidth, 32)];
    image4.image = [UIImage imageNamed:@"img_xiaotieshi"];
    [_scroll addSubview:image4];
    
    UILabel *tips = [[UILabel alloc]initWithFrame:CGRectMake(10,15 + image4.frame.origin.y + image4.frame.size.height, 300, 100)];
    tips.text = [[NSString stringWithFormat:@"%@", _tips] stringByReplacingOccurrencesOfString:@"\\n" withString:@" \r\n"];
    tips.numberOfLines = 0;
    tips.font = [UIFont systemFontOfSize:14];
    [tips sizeToFit];
    [_scroll addSubview:tips];
    
    UIImageView *back = [[UIImageView alloc]initWithFrame:CGRectMake(0,15 + tips.frame.size.height + tips.frame.origin.y, kwidth, 25)];
    back.image = [UIImage imageNamed:@"img_back"];
    [_scroll addSubview:back];
    
    UIImageView *background = [[UIImageView alloc]initWithFrame:CGRectMake(0, back.frame.size.height + back.frame.origin.y, kwidth, kHeight)];
    background.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    [_scroll addSubview:background];
    
    _scroll.contentSize = CGSizeMake(kwidth, back.frame.size.height + back.frame.origin.y);
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView.tag == 1) {
        if (_mainStuffName.count % 2 == 1) {
            return _mainStuffName.count / 2 + 1;
        } else {
            return _mainStuffName.count / 2;
        }
    } else if (tableView.tag == 2) {
        return _stepPhoto.count;
    } else if (tableView.tag == 3) {
        if (_otherStuffName.count % 2 == 1) {
            return _otherStuffName.count / 2 + 1;
        } else {
            return _otherStuffName.count / 2;
        }
    }
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    } else {
        for (UIView *view in [cell.contentView subviews]) {
            [view removeFromSuperview];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (tableView.tag == 1) {
        if (_mainStuffName.count % 2 == 1) {
            if (indexPath.row == _mainStuffName.count / 2) {
                UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
                material1.text = [_mainStuffName objectAtIndex:indexPath.row * 2];
                material1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material1];
                
                UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, 70, 30)];
                quantity1.text = [_mainStuffWeight objectAtIndex:indexPath.row * 2];
                quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                quantity1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:quantity1];
            } else {
                UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
                material1.text = [_mainStuffName objectAtIndex:indexPath.row * 2];
                material1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material1];
                
                UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, 70, 30)];
                quantity1.text = [_mainStuffWeight objectAtIndex:indexPath.row * 2];
                quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                quantity1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:quantity1];
                
                UILabel *material2 = [[UILabel alloc]initWithFrame:CGRectMake(184, 10, 70, 30)];
                material2.text = [_mainStuffName objectAtIndex:indexPath.row * 2 + 1];
                material1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material2];
                
                UILabel *quantity2 = [[UILabel alloc]initWithFrame:CGRectMake(251, 10, 70, 30)];
                quantity2.text = [_mainStuffWeight objectAtIndex:indexPath.row * 2 + 1];
                quantity2.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                quantity2.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:quantity2];
            }
        } else {
            UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
            material1.text = [_mainStuffName objectAtIndex:indexPath.row * 2];
            material1.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:material1];
            
            UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, 70, 30)];
            quantity1.text = [_mainStuffWeight objectAtIndex:indexPath.row * 2];
            quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
            quantity1.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:quantity1];
            
            UILabel *material2 = [[UILabel alloc]initWithFrame:CGRectMake(184, 10, 70, 30)];
            material2.text = [_mainStuffName objectAtIndex:indexPath.row * 2 + 1];
            material2.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:material2];
            
            UILabel *quantity2 = [[UILabel alloc]initWithFrame:CGRectMake(251, 10, 70, 30)];
            quantity2.text = [_mainStuffWeight objectAtIndex:indexPath.row * 2 + 1];
            quantity2.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
            quantity2.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:quantity2];
        }
    } else if (tableView.tag == 2) {
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(7, 5, 80, 60)];
        [image setImageURLStr:[_stepPhoto objectAtIndex:indexPath.row] placeholder:[UIImage imageNamed:@"img_delete_advertisement"]];
        image.clipsToBounds = YES;
        image.contentMode = UIViewContentModeScaleAspectFill;
        [cell.contentView addSubview:image];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(92, 5, 215, 55)];
        label.text = [NSString stringWithFormat:@"%d.%@",indexPath.row + 1,[_stepIntro objectAtIndex:indexPath.row]];
        label.font = [UIFont systemFontOfSize:14];
        label.numberOfLines = 4;
        [cell.contentView addSubview:label];
    } else if (tableView.tag == 3) {
        if (_otherStuffName.count % 2 == 1) {
            if (indexPath.row == _otherStuffName.count / 2) {
                UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
                material1.text = [_otherStuffName objectAtIndex:indexPath.row * 2];
                material1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material1];
                
                UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(94, 10, 70, 30)];
                quantity1.text = [_otherStuffWeight objectAtIndex:indexPath.row * 2];
                quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                quantity1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:quantity1];
            } else {
                UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
                material1.text = [_otherStuffName objectAtIndex:indexPath.row * 2];
                material1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material1];
                
                UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(94, 10, 70, 30)];
                quantity1.text = [_otherStuffWeight objectAtIndex:indexPath.row * 2];
                quantity1.font = [UIFont systemFontOfSize:15];
                quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                [cell.contentView addSubview:quantity1];
                
                UILabel *material2 = [[UILabel alloc]initWithFrame:CGRectMake(186, 10, 70, 30)];
                material2.text = [_otherStuffName objectAtIndex:indexPath.row * 2 + 1];
                material2.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material2];
                
                UILabel *quantity2 = [[UILabel alloc]initWithFrame:CGRectMake(251, 10, 70, 30)];
                quantity2.text = [_otherStuffWeight objectAtIndex:indexPath.row * 2 + 1];
                quantity2.font = [UIFont systemFontOfSize:15];
                quantity2.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                [cell.contentView addSubview:quantity2];
            }
        } else {
            UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
            material1.text = [_otherStuffName objectAtIndex:indexPath.row * 2];
            material1.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:material1];
            
            UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(94, 10, 70, 30)];
            quantity1.text = [_otherStuffWeight objectAtIndex:indexPath.row * 2];
            quantity1.font = [UIFont systemFontOfSize:15];
            quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
            [cell.contentView addSubview:quantity1];
            
            UILabel *material2 = [[UILabel alloc]initWithFrame:CGRectMake(186, 10, 70, 30)];
            material2.text = [_otherStuffName objectAtIndex:indexPath.row * 2 + 1];
            material2.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:material2];
            
            UILabel *quantity2 = [[UILabel alloc]initWithFrame:CGRectMake(251, 10, 70, 30)];
            quantity2.text = [_otherStuffWeight objectAtIndex:indexPath.row * 2 + 1];
            quantity2.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
            quantity2.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:quantity2];
        }
    }
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
